﻿using GalaSoft.MvvmLight;
using MyImdb.Common;
using MyImdb.Models;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;

namespace MyImdb.ViewModel
{
    internal class StartViewModel : ViewModelBase
    {
        /// <summary>
        /// The _ imdb titles
        /// </summary>
        private ObservableCollection<ImdbItem> _ImdbTitles;
        /// <summary>
        /// Gets or sets the imdb titles.
        /// </summary>
        /// <value>
        /// The imdb titles.
        /// </value>
        public ObservableCollection<ImdbItem> ImdbTitles
        {
            get { return _ImdbTitles; }
            set
            {
                _ImdbTitles = value;
                RaisePropertyChanged("ImdbTitles");
            }
        }

        /// <summary>
        /// The _ loading visibility
        /// </summary>
        private Visibility _LoadingVisibility;
        /// <summary>
        /// Gets or sets the loading visibility.
        /// </summary>
        /// <value>
        /// The loading visibility.
        /// </value>
        public Visibility LoadingVisibility
        {
            get { return _LoadingVisibility; }
            set
            {
                _LoadingVisibility = value;
                RaisePropertyChanged("LoadingVisibility");
            }
        }

        /// <summary>
        /// The _ local titles count
        /// </summary>
        private int _LocalTitlesCount;
        /// <summary>
        /// Gets or sets the local titles count.
        /// </summary>
        /// <value>
        /// The local titles count.
        /// </value>
        public int LocalTitlesCount
        {
            get { return _LocalTitlesCount; }
            set
            {
                _LocalTitlesCount = value;
                RaisePropertyChanged("LocalTitlesCount");
            }
        }

        /// <summary>
        /// The _ TV series count
        /// </summary>
        private int _TVSeriesCount;
        /// <summary>
        /// Gets or sets the TV series count.
        /// </summary>
        /// <value>
        /// The TV series count.
        /// </value>
        public int TVSeriesCount
        {
            get { return _TVSeriesCount; }
            set
            {
                _TVSeriesCount = value;
                RaisePropertyChanged("TVSeriesCount");
            }
        }

        /// <summary>
        /// The _ episodes count
        /// </summary>
        private int _EpisodesCount;
        /// <summary>
        /// Gets or sets the episodes count.
        /// </summary>
        /// <value>
        /// The episodes count.
        /// </value>
        public int EpisodesCount
        {
            get { return _EpisodesCount; }
            set
            {
                _EpisodesCount = value;
                RaisePropertyChanged("EpisodesCount");
            }
        }

        /// <summary>
        /// The _ movies count
        /// </summary>
        private int _MoviesCount;
        /// <summary>
        /// Gets or sets the movies count.
        /// </summary>
        /// <value>
        /// The movies count.
        /// </value>
        public int MoviesCount
        {
            get { return _MoviesCount; }
            set
            {
                _MoviesCount = value;
                RaisePropertyChanged("MoviesCount");
            }
        }

        /// <summary>
        /// The _ season titles
        /// </summary>
        private List<string> _SeasonTitles;
        /// <summary>
        /// Gets or sets the season titles.
        /// </summary>
        /// <value>
        /// The season titles.
        /// </value>
        public List<string> SeasonTitles
        {
            get { return _SeasonTitles; }
            set
            {
                _SeasonTitles = value;
                RaisePropertyChanged("SeasonTitles");
            }
        }

        private Settings _AppSettings;
        public Settings AppSettings
        {
            get { return _AppSettings; }
            set
            {
                _AppSettings = value;
                RaisePropertyChanged("AppSettings");
            }
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="StartViewModel" /> class.
        /// </summary>
        public StartViewModel()
        {
            ImdbTitles = new ObservableCollection<ImdbItem>();
            SeasonTitles = new List<string>();
            ReadSettings();
            Start();
        }

        private void ReadSettings()
        {
            AppSettings = FileHelper.ReadSettings();
            FileHelper.DatabaseBasePath = AppSettings.DatabasePath;
        }

        /// <summary>
        /// Starts reading titles.
        /// </summary>
        private void Start()
        {
            if (FileHelper.IsDatabaseExist(AppSettings.DatabasePath))
            {
                FileHelper.DatabaseBasePath = AppSettings.DatabasePath;

                var titleList = FileHelper.ReadTitles();
                if (titleList != null)
                {
                    foreach (var title in titleList)
                    {
                        var imdbTitle = FileHelper.ReadTitle(title);
                        if (imdbTitle != null)
                        {
                            ImdbTitles.Add(imdbTitle);
                        }
                    }

                    LocalTitlesCount = ImdbTitles.Count();

                    var tvSeasons = ImdbTitles.Where(t => t.Episodes != null);

                    TVSeriesCount = tvSeasons.Count();
                    MoviesCount = LocalTitlesCount - TVSeriesCount;
                    EpisodesCount = tvSeasons.Sum(t => t.Episodes.Count);

                    foreach (var season in tvSeasons)
                    {
                        SeasonTitles.Add(season.Title);
                    }
                    SeasonTitles.Sort();
                } 
            }
            else
            {
                //Database path doesn't exist
            }
        }
    }
}
